package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

@Autowired
private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealDTO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //先将套餐进行插入操作
    Setmeal setmeal=new Setmeal();
    BeanUtils.copyProperties(setmealDTO,setmeal);
    setmealMapper.insert(setmeal);
        //获取插入套餐的套餐ID
    Long setmealId=setmeal.getId();
        //然后通过套餐的id 向setmeal_dish表中进行赋值套餐的ID
    List<SetmealDish> list=setmealDTO.getSetmealDishes();
    for(SetmealDish setmealDish:list){
    setmealDish.setSetmealId(setmealId);
    //调用方法向setmeal_dish关系表插入赋值后的完整数据
    setmealMapper.insertSetmealWithDish(setmealDish);
}
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //查看是否在售
        ids.forEach(id->{
            Integer  status=setmealMapper.getStatusByid(id);
            if (status==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //删除套餐以及关联套餐
        ids.forEach(setmealId->{
            setmealMapper.deleteById(setmealId);
            setmealMapper.setmealDish(setmealId);
        });
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal=Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }


    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal=setmealMapper.getById(id);
        if (setmeal==null){
            throw  new SetmealEnableFailedException(MessageConstant.UNKNOWN_ERROR);
        }
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> list=setmealMapper.getDishById(id);
        BeanUtils.copyProperties(list,setmealVO);
        return setmealVO;
    }
//修改套餐代码
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐表信息
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //2.删除关联表相关菜品信息
        Long setmealId=setmealDTO.getId();

        setmealDishMapper.deleteById(setmealId);//删除关联表信息
        //3.修改关联表信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public List<SetmealVO> listWithDish(Setmeal setmeal) {
        List<Setmeal> setmealList=setmealMapper.list(setmeal);
        List<SetmealVO> setmealVOList=new ArrayList<>();
        for (Setmeal s:setmealList){
            SetmealVO setmealVO=new SetmealVO();
            BeanUtils.copyProperties(s,setmealVO);
            List<SetmealDish> setmealDishes=setmealDishMapper.getSetmealWithDish(s.getId());
            setmealVO.setSetmealDishes(setmealDishes);
            setmealVOList.add(setmealVO);
        }
        return setmealVOList;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
